import {
    VehicleDetail
} from './vehicle-detail.model'
import {
    Config
} from '../../utils/config'
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
const vehicleDetail = new VehicleDetail()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        vehicle: '',
        fromPage: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this,
            id = options.id,
            eventChannel = this.getOpenerEventChannel()
        if (id != undefined) {
            that.setData({
                id: id
            })
            that.loadData(id)
        }
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            if ('order' == data.fromPage) {
                that.setData({
                    fromPage: 'order'
                })
            }
        })
    },

    /**
     * 获取所有数据
     */
    loadData(id) {
        let that = this
        that.getLicenseData(id)
    },

    /**
     * 获取行驶证详情
     */
    getLicenseData(id) {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this
        vehicleDetail.getLicenseData(id).then((res) => {
            wx.hideLoading()
            that.setData({
                vehicle: res.data
            })
        })
    },

    /**
     * 点击拍照获取行驶证信息
     */
    onTapOcr() {
        let that = this,
            url = Config.baseUrl + 'image'
        vehicleDetail.chooseImage().then((tempFilePaths) => {
            vehicleDetail.uploadFile(url, tempFilePaths).then((res) => {
                let data = JSON.parse(res.data)
                // 通过照片获取行驶证信息
                that.ocrDriving(data.path)
            })
        })
    },

    /**
     * 通过照片获取行驶证信息
     * @param {*} url 上传的图片地址
     */
    ocrDriving(url) {
        let that = this,
            param = {
                image: url
            }
        vehicleDetail.ocrDriving(param).then((res) => {
            wx.hideLoading()
            that.setData({
                vehicle: res.data,
            })
        })
    },

    /**
     * 提交表单
     * @param {*} options
     */
    doSubmit(options) {
        let that = this,
            errMsg = '',
            id = that.data.id,
            fromPage = that.data.fromPage,
            param = options.detail.value,
            prevPage = vehicleDetail.getPrevPage() // 获取上一页
        if (param.license_register_date && !Config.registerDate.test(param.license_register_date)) {
            errMsg = '注册日期格式不正确'
        }
        if (!param.license_register_date) {
            errMsg = '注册日期不能为空'
        }
        if (!param.engine_identification) {
            errMsg = '发动机号码不能为空'
        }
        if (!param.vehicle_identification) {
            errMsg = '识别代号不能为空'
        }
        if (!param.user_address) {
            errMsg = '住址不能为空'
        }
        if (!param.user_name) {
            errMsg = '所有人不能为空'
        }
        if (!param.license_typename) {
            errMsg = '车辆类型不能为空'
        }
        if (param.license_number && !Config.carNumber.test(param.license_number)) {
            errMsg = '号牌号码格式不正确'
        }
        if (!param.license_number) {
            errMsg = '号牌号码不能为空'
        }
        if (errMsg) {
            Toast(errMsg)
            return false
        }
        if (id) {
            vehicleDetail.updateLicenseData(id, param).then(() => {
                Toast({
                    type: 'success',
                    message: '提交成功',
                    onClose: () => {
                        prevPage._loadData()
                        wx.navigateBack({
                            delta: 1
                        })
                    },
                })
            })
        } else {
            vehicleDetail.insertLicenseData(param).then((res) => {
                Toast({
                    type: 'success',
                    message: '提交成功',
                    onClose: () => {
                        if ('order' == fromPage) {
                            let licenseList = prevPage.data.licenseList,
                                licenseIndex = prevPage.data.licenseIndex
                            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                            licenseList.unshift(res.data.license_number)
                            licenseIndex.unshift(res.data.id)
                            prevPage.setData({
                                licenseList: licenseList,
                                licenseIndex: licenseIndex
                            })
                        } else {
                            prevPage._loadData()
                        }
                        wx.navigateBack({
                            delta: 1
                        })
                    },
                });
            })
        }
    },

    /**
     * 删除行驶证
     */
    onDelete() {
        let that = this,
            prevPage = vehicleDetail.getPrevPage() // 获取上一页
        Dialog.confirm({
            title: '提示',
            message: '是否确认删除？',
        }).then(() => {
            vehicleDetail.deleteLicense(that.data.id).then(() => {
                Toast({
                    type: 'success',
                    message: '删除成功',
                    onClose: () => {
                        prevPage._loadData()
                        wx.navigateBack({
                            delta: 1
                        })
                    },
                });
            })
        }).catch(() => {
            // on cancel
        })
    }
})